package com.lagou;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jsy
 * @description: TODO
 * @date 2021/8/28 19:13
 */
public class test {

    /**
     * 查询
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().getBuild(inputStream);
        SqlSession sqlSession= sqlSessionFactory.openSession();
        User user = new User();
        user.setId(2);
        user.setUsername("tom");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> users = new ArrayList<>();
        User user1 = new User();
        try {
            users= userDao.selectList();
            user1= userDao.selectOne(user);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("条件查询："+user1);
            System.out.println("查询所有如下："+users);
        }
    }

    /**
     * 更新
     *
     * @throws Exception
     */

    @Test
    public void testUpdate() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().getBuild(inputStream);
        SqlSession sqlSession= sqlSessionFactory.openSession();
        User user = new User();
        user.setId(2);
        user.setUsername("老表");
        user.setBirthday("2000-01-01");
        user.setPassword("20000101");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int result = userDao.update(user);
        System.out.println("更新结果："+result);
    }

    /**
     * 添加
     *
     * @throws Exception
     */
    @Test
    public void testInsert() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().getBuild(inputStream);
        SqlSession sqlSession= sqlSessionFactory.openSession();
        User user = new User();
        user.setUsername("老铁");
        user.setBirthday("2021-09-05");
        user.setPassword("99999");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int result = userDao.insert(user);
        System.out.println("添加结果："+result);
    }

    /**
     * 删除
     *
     * @throws Exception
     */

    @Test
    public void testDelete() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().getBuild(inputStream);
        SqlSession sqlSession= sqlSessionFactory.openSession();
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int result = userDao.delete(1);
        System.out.println("删除结果："+result);
    }
}
